package zhr.chapter12;

import java.util.Stack;

public class ReadStack implements Runnable {

    Stack<Integer> myStack;
    int count = 0;

    public ReadStack(Stack<Integer> myStack) {
        this.myStack = myStack;
    }

    @Override
    public void run() {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("子线程" + Thread.currentThread().getName() + "开始读数据");
        while (count < 10) {
            synchronized (myStack) {
                if (myStack.empty()) {
                    try {
                        myStack.wait();
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                }
                System.out.println("子线程" + Thread.currentThread().getName() + "读数据" + myStack.pop());

                count++;
                if (myStack.empty() || count == 10){
                    myStack.notify();
                }
            }
        }

    }

}
